import { findIndex } from 'lodash'
export function getTreeData(dataSource: any[]) {
	const treeData: any[] = [], childrenData: any[] = [];
	for (let item of dataSource) {
		if (item.parentId) {
			const index = findIndex(treeData, { id: item.parentId });
			index >= 0 ? treeData[index].children.push(item) : childrenData.push(item);
		} else {
			treeData.push({ ...item, disabled: true, children: [] })
		}
	};
	for (let childe of childrenData) {
		const { parentId } = childe;
		const index = findIndex(treeData, { id: parentId });
		if (index >= 0) {
			treeData[index].children.push(childe)
		}
	}
	return treeData
}

export function getTreeDataSelect(dataSource: any[]) {
	const treeData: any[] = [], childrenData: any[] = [];
	for (let item of dataSource) {
		if (item.parentId) {
			const index = findIndex(treeData, { id: item.parentId });
			index >= 0 ? treeData[index].children.push({ value: item.id, key: item.id, title: item.name }) : childrenData.push(item);
		} else {
			treeData.push({ value: item.id, key: item.id, title: item.name, selectable: false, children: [] })
		}
	};
	for (let childe of childrenData) {
		const { parentId } = childe;
		const index = findIndex(treeData, { value: parentId });
		if (index >= 0) {
			treeData[index].children.push({ value: childe.id, key: childe.id, title: childe.name })
		}
	}
	return treeData

}